﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.销售确认单;
using DXApplicationTangche.UC.门店下单.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.UC.下单MTO.Service
{
    public class MTO订单Service
    {
        /// <summary>
        /// 下单面料一览
        /// </summary>
        /// <returns></returns>
        public static List<面料选择> getMaterialOrderIndexAll()
        {
            return
                FunctionHelper.JsonDeserialization<List<面料选择>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/MTOOrder/customized/getMaterialOrderIndexAll"
                    )
                );
        }
        /// <summary>
        /// 根据吊牌查找订单信息
        /// </summary>
        public static OrderCommitDTO返回 getOrderCommitDtoByBarcode(String barcode)
        {
            return
                FunctionHelper.JsonDeserialization<OrderCommitDTO返回>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/MTOOrder/sshirtTag/getOrderCommitDtoByBarcode?barcode=" + barcode)
                    );
        }

        /// <summary>
        /// 创建销售订单
        /// </summary>
        /// <param name="saleOrderFacadeDTO"></param>
        /// <returns></returns>
        public static String createSaleOrderAndSaveRecordeSaleOrder(String mnemonic, SaleOrderRequestFacadeDTO saleOrderRequestFacade)
        {
            return
                RestCall.PostHttp(
                        URL.接口url + "/MTOOrder/sshirtTag/createSaleOrderAndSaveRecord?mnemonic=" + mnemonic,
                        FunctionHelper.JsonSerialization(saleOrderRequestFacade)
                    );
        }
        /// <summary>
        /// 客套整合下单界面创建销售订单
        /// </summary>
        /// <param name="saleOrderFacadeDTO"></param>
        /// <returns></returns>
        public static String createSaleOrderAndSaveRecordWith(String mnemonic, String sales, String paymentType, SaleOrderRequestFacadeDTO saleOrderRequestFacade)
        {
            return
                RestCall.PostHttp(
                        URL.接口url + "/MTOOrder/sshirtTag/createSaleOrderAndSaveRecordWith?mnemonic=" + mnemonic + "&sales=" + sales + "&paymentType=" + paymentType,
                        FunctionHelper.JsonSerialization(saleOrderRequestFacade)
                    );
        }
        /// <summary>
        /// 根据styleNameCn取尺码组
        /// </summary>
        /// <param name="styleNameCn"></param>
        /// <returns></returns>
        public static List<String> findSizeCdByStyleNameCn(String styleNameCn)
        {
            return
                FunctionHelper.JsonDeserialization<List<String>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/MTOOrder/customized/findSizeCdByStyleNameCn?styleNameCn=" + styleNameCn
                        )
                    );
        }


        /// <summary>
        /// 保存团体客户
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="customerName"></param>
        /// <param name="addressProvice"></param>
        /// <param name="addressCity"></param>
        /// <param name="addressDistrict"></param>
        /// <param name="addressDetail"></param>
        /// <param name="zipCode"></param>
        /// <param name="remarks"></param>
        /// <returns></returns>
        public static CustomerPDTO saveCustomerP(String mobile, String customerName, String addressProvince, String addressCity, String addressDistrict, String addressDetail, String zipCode, String remarks)
        {
            return
                FunctionHelper.JsonDeserialization<CustomerPDTO>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/saveCustomerP?mobile=" + mobile
                        + "&customerName=" + customerName
                        + "&addressProvince=" + addressProvince
                        + "&addressCity=" + addressCity
                        + "&addressDistrict=" + addressDistrict
                        + "&addressDetail=" + addressDetail
                        + "&zipCode=" + zipCode
                        + "&remarks=" + remarks)
                    );
        }
        /// <summary>
        /// 根据电话查询团体客户
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static CustomerPDTO getCustomerPByMobile(String mobile)
        {
            return
                FunctionHelper.JsonDeserialization<CustomerPDTO>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/getCustomerPByMobile?mobile=" + mobile)
                    );
        }
        /// <summary>
        /// 判断是否存在会员账户
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static Boolean isMemberRecharge(String mobile)
        {
            return
                FunctionHelper.JsonDeserialization<Boolean>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/isMemberRecharge?mobile=" + mobile)
                    );
        }

        /// <summary>
        /// 查询所有客户
        /// </summary>
        /// <returns></returns>
        public static List<CustomerPDTO> findAllCustomers()
        {
            return
                FunctionHelper.JsonDeserialization<List<CustomerPDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/findAllCustomers"
                        )
                    );
        }
        /// <summary>
        /// 查询全部客户带会员类型
        /// </summary>
        /// <returns></returns>
        public static List<CustomerPDTO> findAllCustomersWIthMembershipType()
        {
            return
                FunctionHelper.JsonDeserialization<List<CustomerPDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/findAllCustomersWIthMembershipType"
                        )
                    );
        }
        /// <summary>
        /// 根据电话查询客户量体
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static List<CustomerFitRDTO量体值> getCustomerFitRDTOsByMobile(String mobile)
        {
            try
            {
                return
                   FunctionHelper.JsonDeserialization<List<CustomerFitRDTO量体值>>(
                       RestCall.httpGetMethod(
                           URL.接口url + "/customer/getCustomerFitRDTOsByMobile?mobile=" + mobile
                           )
                       );
            }
            catch
            {
                return new List<CustomerFitRDTO量体值>();
            }
        }
        /// <summary>
        /// 保存客户量体信息
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="customerFitRDTOs"></param>
        /// <returns></returns>
        public static List<CustomerFitRDTO量体值> saveCustomerFitRByMobile(String mobile, List<CustomerFitRDTO量体值> customerFitRDTOs)
        {
            try
            {
                return
                   FunctionHelper.JsonDeserialization<List<CustomerFitRDTO量体值>>(
                       RestCall.PostHttp(
                           URL.接口url + "/customer/saveCustomerFitRByMobile?mobile=" + mobile,
                           FunctionHelper.JsonSerialization(customerFitRDTOs)
                           )
                       );
            }
            catch
            {
                return new List<CustomerFitRDTO量体值>();
            }
        }
        /// <summary>
        /// 销售订单确认:加急或待确认
        /// </summary>
        /// <param name="saleOrderComfirm"></param>
        /// <returns></returns>
        public static PlainMessageResult saleOrderComfirm(SaleOrderComfirmDTO saleOrderComfirm)
        {
            return
                FunctionHelper.JsonDeserialization<PlainMessageResult>(
                       RestCall.PostHttp(
                           URL.接口url + "/MTOOrder/sshirtTag/saleOrderComfirm",
                           FunctionHelper.JsonSerialization(saleOrderComfirm)
                           )
                       );
        }
        /// <summary>
        /// 取带用户画像的客户一览
        /// </summary>
        /// <returns></returns>
        public static List<CustomerPersonaListDto> getCustomerPersonaListAll()
        {
            return
                FunctionHelper.JsonDeserialization<List<CustomerPersonaListDto>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/getCustomerPersonaListAll"
                        )
                    );
        }

        /// <summary>
        /// 销售订单作废
        /// </summary>
        /// <returns></returns>
        public static PlainMessageResult saleOrderDeprecate(String saleOrderDetailId)
        {
            return
                FunctionHelper.JsonDeserialization<PlainMessageResult>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/MTOOrder/sshirtTag/saleOrderDeprecate?saleOrderDetailId=" + saleOrderDetailId
                        )
                    );
        }

        /// <summary>
        /// 根据上级地区找下级
        /// </summary>
        /// <returns></returns>
        public static List<AreaDetailDTO> getAreaDetail(String treePath)
        {
            return
                FunctionHelper.JsonDeserialization<List<AreaDetailDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/area/getAreaDetail?treePath=" + treePath
                        )
                    );
        }
    }
    public class AreaDetailDTO
    {
        public String areaId { get; set; }
        public String name { get; set; }
        public String parentCode { get; set; }
        public String parentLevelName { get; set; }
        public AreaDetailDTO()
        {

        }
    }

    public class SaleOrderRequestFacadeDTO
    {
        public SaleOrderTitleRequstDTO saleOrderTitleRequstDTO { get; set; } = new SaleOrderTitleRequstDTO();
        public List<SaleOrderDetailRequestDTO> saleOrderDetailRequestDTOList { get; set; } = new List<SaleOrderDetailRequestDTO>();
        public SaleOrderRequestFacadeDTO()
        {

        }
        public SaleOrderRequestFacadeDTO(
            SaleOrderTitleRequstDTO saleOrderTitleRequstDTO,
            List<SaleOrderDetailRequestDTO> saleOrderDetailRequestDTOList
            )
        {
            this.saleOrderTitleRequstDTO = saleOrderTitleRequstDTO;
            this.saleOrderDetailRequestDTOList = saleOrderDetailRequestDTOList;
        }
    }
    public class SaleOrderTitleRequstDTO
    {
        public String saleOrderType { get; set; }//  单位团体、个人
        public String storeId { get; set; } //门店id
        public String assistantId { get; set; }//业务员id
        public CustomerOrderCommitDTO customerOrderCommitDTO { get; set; }
        public DateTime deliveryDate { get; set; }//  交期
        public Double payment { get; set; }
        public String saleType { get; set; }//  销售方式
        public String fetchStyle { get; set; }//  交货方式
        public String payStyle { get; set; }//  付款方式
        public SaleOrderTitleRequstDTO()
        {

        }

        public SaleOrderTitleRequstDTO(
            string saleType,
            string storeId,
            string assistantId,
            DateTime deliveryDate)
        {
            this.storeId = storeId;
            this.assistantId = assistantId;
            this.deliveryDate = deliveryDate;
            this.saleType = saleType;
        }
    }
    public class SaleOrderDetailRequestDTO
    {
        public String refBarcode { get; set; }
        public Double orderNumber { get; set; }
        public String remarks { get; set; }
        public String styleSizeCd { get; set; }
        public String styleNameCn { get; set; }
        public String materialCode { get; set; }
        public CustomerOrderCommitDTO customerOrderCommitDTO { get; set; }
        public SaleOrderDetailRequestDTO()
        {

        }

        public SaleOrderDetailRequestDTO(
            string refBarcode,
            double orderNumber,
            string remarks,
            string styleSizeCd,
            CustomerOrderCommitDTO customerOrderCommitDTO,
            string styleNameCn,
            string materialCode)
        {
            this.refBarcode = refBarcode;
            this.orderNumber = orderNumber;
            this.remarks = remarks;
            this.styleSizeCd = styleSizeCd;
            this.customerOrderCommitDTO = customerOrderCommitDTO;
            this.styleNameCn = styleNameCn;
            this.materialCode = materialCode;
        }
    }
    public class CustomerOrderCommitDTO
    {
        public String customerId { get; set; }//客户id
        public String customerName { get; set; }//客户名称
        public String mobile { get; set; }  //手机
        public String addressId { get; set; }//地址id
        public String addressProvince { get; set; }//省
        public String addressCity { get; set; }//市
        public String addressDistrict { get; set; }//区
        public String addressDetail { get; set; }//详细地址
        public String zipCode { get; set; }//邮编
        public CustomerOrderCommitDTO()
        {

        }
        public CustomerOrderCommitDTO(CustomerPDTO customerPDTO)
        {
            this.customerName = customerPDTO.customerName;
            this.mobile = customerPDTO.mobile;
            //this.addressId = addressId;
            this.addressProvince = customerPDTO.addressProvince;
            this.addressCity = customerPDTO.addressCity;
            this.addressDistrict = customerPDTO.addressDistrict;
            this.addressDetail = customerPDTO.addressDetail;
            this.zipCode = customerPDTO.zipCode;
        }
        public CustomerOrderCommitDTO(CustomerPDTOWithBalance customerPDTO)
        {
            this.customerName = customerPDTO.customerName;
            this.mobile = customerPDTO.mobile;
            //this.addressId = addressId;
            this.addressProvince = customerPDTO.addressProvince;
            this.addressCity = customerPDTO.addressCity;
            this.addressDistrict = customerPDTO.addressDistrict;
            this.addressDetail = customerPDTO.addressDetail;
            this.zipCode = customerPDTO.zipCode;
        }
        public CustomerOrderCommitDTO(
            //string customerId, 
            string customerName,
            string mobile,
            //string addressId, 
            string addressProvince,
            string addressCity,
            string addressDistrict,
            string addressDetail,
            string zipCode)
        {
            //this.customerId = customerId;
            this.customerName = customerName;
            this.mobile = mobile;
            //this.addressId = addressId;
            this.addressProvince = addressProvince;
            this.addressCity = addressCity;
            this.addressDistrict = addressDistrict;
            this.addressDetail = addressDetail;
            this.zipCode = zipCode;
        }
    }

    public class CustomerPDTOWithBalance : CustomerPDTO
    {
        public Double balance { get; set; } //  余额
        public CustomerPDTOWithBalance()
        {

        }
    }
    public class CustomerPDTO
    {
        public String mobile { get; set; }
        public String customerName { get; set; }
        public String addressProvince { get; set; }
        public String addressCity { get; set; }
        public String addressDistrict { get; set; }
        public String addressDetail { get; set; }
        public String zipCode { get; set; }
        public String remarks { get; set; }
        public String membershipType { get; set; }
        public CustomerPDTO()
        {

        }

        public CustomerPDTO(CustomerPDTOWithBalance customerPDTOWithBalance)
        {
            this.mobile = customerPDTOWithBalance.mobile;
            this.customerName = customerPDTOWithBalance.customerName;
            this.addressProvince = customerPDTOWithBalance.addressProvince;
            this.addressCity = customerPDTOWithBalance.addressCity;
            this.addressDistrict = customerPDTOWithBalance.addressDistrict;
            this.addressDetail = customerPDTOWithBalance.addressDetail;
            this.zipCode = customerPDTOWithBalance.zipCode;
            this.remarks = customerPDTOWithBalance.remarks;
        }
        public CustomerPDTO build(
            String mobile,
            String customerName,
            String addressProvince,
            String addressCity,
            String addressDistrict,
            String addressDetail)
        {
            this.mobile = mobile;
            this.customerName = customerName;
            this.addressProvince = addressProvince;
            this.addressCity = addressCity;
            this.addressDistrict = addressDistrict;
            this.addressDetail = addressDetail;
            return this;
        }
        public String buildAddress()
        {
            return isNull(this.addressProvince) +
                isNull(this.addressCity) +
                isNull(addressDistrict) +
                isNull(addressDetail) +
                isNull(zipCode);
        }

        public CustomerPDTO build(string customerName, string addressProvince, string addressCity, string addressDistrict, string addressDetail)
        {
            this.customerName = customerName;
            this.addressProvince = addressProvince;
            this.addressCity = addressCity;
            this.addressDistrict = addressDistrict;
            this.addressDetail = addressDetail;
            return this;
        }
        private String isNull(String str)
        {
            return String.IsNullOrEmpty(str) ? "" : str;
        }
    }
    /// <summary>
    /// 客户信息带客户认证信息
    /// </summary>
    public class CustomerPWithIdentityAndScoreDTO
    {
        public CustomerPDTO customerPDTO { get; set; } = new CustomerPDTO();
        public CustomerIdentityDTO customerIdentityDTO { get; set; } = new CustomerIdentityDTO();
        public int confirmdScore { get; set; }
        public CustomerPWithIdentityAndScoreDTO()
        {

        }
    }
    public class CustomerIdentityDTO
    {
        public String memberId { get; set; }   //  基础会员号
        public String customerType { get; set; }    //  会员类别
        public String responser { get; set; }   //  销售负责人
        public String saleSource { get; set; } //  客户来源：门店，团体，抖音
        public CustomerIdentityDTO()
        {

        }
    }
    public class SaleOrderComfirmDTO
    {
        public String detailId { get; set; }
        public Boolean urgent { get; set; }
        public Boolean toBeConfirm { get; set; }
        public SaleOrderComfirmDTO()
        {

        }
        public SaleOrderComfirmDTO(String detailId)
        {
            this.detailId = detailId;
        }

        public SaleOrderComfirmDTO buildUrgent()
        {
            this.urgent = true;
            return this;
        }

        public SaleOrderComfirmDTO buildToBeConfirm()
        {
            this.toBeConfirm = true;
            return this;
        }

    }

    public class ConstPlainMessageResult
    {
        public const String OK = "200";  //  正常
        public const String NOT_FOUND = "404";  //  没有值
        public const String GONE = "410";  //    值无效
        public const String INTENAL_SERVER_ERROR = "500";  //    内部程序错误
        public const String METHOD_NOT_ALLOWED = "405";  //    Method Not Allowed
    }

    public class PlainMessageResult
    {
        public String errorCode { get; set; }
        public String errorMessage { get; set; }
        public PlainMessageResult()
        {

        }

        public PlainMessageResult(string errorCode, string errorMessage)
        {
            this.errorCode = errorCode;
            this.errorMessage = errorMessage;
        }
    }
    public class CustomerPersonaListDto
    {
        public String mobile { get; set; }
        public String customerName { get; set; }
        public String addressProvince { get; set; }
        public String addressCity { get; set; }
        public String addressDistrict { get; set; }
        public String addressDetail { get; set; }
        public String zipCode { get; set; }
        public String remarks { get; set; }

        /**
         * 客户基础信息
         */
        public String wechatName { get; set; } //  微信名
        public DateTime birthday { get; set; }   //  生日
        /**
         * 客户身材信息
         */
        public Double height { get; set; }     //     身高
        public Double weight { get; set; }     //     体重
        public String salesConsultantId { get; set; }   //     销售顾问Id  
        public String salesConsultantName { get; set; }    //     销售顾问
        public CustomerPersonaListDto()
        {

        }
    }
}
